package de.sp.games.snake12k;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class OptionsState extends BasicGameState
{
	private int stateID = 3;

	private Image backgroundImage;
	private Image opImage;

	private UnicodeFont uFBigger;
	private int selected = 0;
	private int time = 0;

	public OptionsState(int stateID)
	{
		this.stateID = stateID;
	}

	public int getID()
	{
		return stateID;
	}

	@SuppressWarnings("unchecked")
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
	{
		backgroundImage = new Image("ressource/box_empty.jpg");
		opImage = new Image("ressource/options.png");
		backgroundImage.setFilter(Image.FILTER_NEAREST);

		uFBigger = new UnicodeFont(new java.awt.Font("Verdana", java.awt.Font.BOLD, 30));
		uFBigger.getEffects().add(new ColorEffect(java.awt.Color.YELLOW));
		uFBigger.addAsciiGlyphs();
		uFBigger.loadGlyphs();
	}

	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
	{
		Input input = gc.getInput();

		if (input.isKeyPressed(Input.KEY_ESCAPE))
		{
			System.out.println("hier!");
			sbg.enterState(Snake12k.TITLEMENUSTATE);
			input.clearKeyPressedRecord();
		}

		if (input.isKeyPressed(Input.KEY_UP))
		{
			if (selected > 0)
			{
				selected--;
			}
		}
		else if (input.isKeyPressed(Input.KEY_DOWN))
		{
			if (selected < 3)
			{
				selected++;
			}
		}
		else if (input.isKeyPressed(Input.KEY_ADD) || input.isKeyPressed(Input.KEY_RIGHT))
		{
			switch (selected)
			{
			case 0:
				if (SUtil.snakeLength < 10)
				{
					SUtil.snakeLength += 1;
				}
				break;
			case 1:
				if (SUtil.picUpGrowValue < 8)
				{
					SUtil.picUpGrowValue += 1;
				}
				break;
			case 2:
				if (GameplayState.fieldX < 32 && GameplayState.fieldY < 32)
				{
					GameplayState.fieldX += 1;
					GameplayState.fieldY += 1;
					SUtil.fieldSize += 1;
					SUtil.fieldResetWithNewField();
				}
				break;
			case 3:
				if (SUtil.moveTime == 16)
				{
					SUtil.moveTime = 12;
				}
				else if (SUtil.moveTime == 12)
				{
					SUtil.moveTime = 9;
				}
				else if (SUtil.moveTime == 9)
				{
					SUtil.moveTime = 6;
				}
				break;
			}
		}
		else if (input.isKeyPressed(Input.KEY_SUBTRACT) || input.isKeyPressed(Input.KEY_LEFT))
		{
			switch (selected)
			{
			case 0:
				if (SUtil.snakeLength > 1)
				{
					SUtil.snakeLength -= 1;
				}
				break;
			case 1:
				if (SUtil.picUpGrowValue > 1)
				{
					SUtil.picUpGrowValue -= 1;
				}
				break;
			case 2:
				if (GameplayState.fieldX > 12 && GameplayState.fieldY > 12)
				{
					GameplayState.fieldX -= 1;
					GameplayState.fieldY -= 1;
					SUtil.fieldSize -= 1;
				}
				break;
			case 3:
				if (SUtil.moveTime == 6)
				{
					SUtil.moveTime = 9;
				}
				else if (SUtil.moveTime == 9)
				{
					SUtil.moveTime = 12;
				}
				else if (SUtil.moveTime == 12)
				{
					SUtil.moveTime = 16;
				}
				break;
			}
		}
	}

	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
	{
		g.scale((1.0f / 16 * gc.getWidth()), (1.0f / 16) * (gc.getHeight() - 30));
		backgroundImage.draw();
		g.resetTransform();

		opImage.draw((gc.getWidth() / 2) - (opImage.getWidth() / 2), 35);

		String msg = ">";
		switch (selected)
		{
		case 0:
			msg = "Start Length - " + (SUtil.snakeLength);
			uFBigger.drawString(((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2) - 41), 200 - (int) Math.abs(Math.sin(time * 0.11) * 10), ">");
			break;
		case 1:
			msg = "Grow Value for PicUp - " + SUtil.picUpGrowValue;
			uFBigger.drawString(((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2) - 41), 300 - (int) Math.abs(Math.sin(time * 0.12) * 10), ">");
			break;
		case 2:
			msg = "Field - " + GameplayState.fieldX;
			uFBigger.drawString(((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2) - 41), 400 - (int) Math.abs(Math.sin(time * 0.13) * 10), ">");
			break;
		case 3:
			switch (SUtil.moveTime)
			{
			case 6:
				msg = "Speed - fastest";
				break;
			case 9:
				msg = "Speed - faster";
				break;
			case 12:
				msg = "Speed - normal";
				break;
			case 16:
				msg = "Speed - slowest";
				break;
			}
			uFBigger.drawString(((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2) - 41), 500 - (int) Math.abs(Math.sin(time * 0.13) * 10), ">");
			break;
		}

		msg = "Start Length - " + String.valueOf(SUtil.snakeLength);
		uFBigger.drawString((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2), 200, msg);

		msg = "Grow Value from PicUp - " + String.valueOf(SUtil.picUpGrowValue);
		uFBigger.drawString((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2), 300, msg);

		msg = "Field - " + String.valueOf(GameplayState.fieldX);
		uFBigger.drawString((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2), 400, msg);

		switch (SUtil.moveTime)
		{
		case 6:
			msg = "Speed - fastest";
			break;
		case 9:
			msg = "Speed - faster";
			break;
		case 12:
			msg = "Speed - normal";
			break;
		case 16:
			msg = "Speed - slowest";
			break;
		}
		uFBigger.drawString((gc.getWidth() / 2) - (uFBigger.getWidth(msg) / 2), 500, msg);

		g.drawString("Ver: " + Snake12k.version + "      Use <-/-> to change the value       'ESC' for save", 10, gc.getHeight() - 25);
	}
}
